﻿using System.Data.Entity;
using System.Security.Principal;
using System.Web;
using Arch.Data;
using Arch.EF.Data;
using Arch.IoC;
using Arch.Samples.Data;
using Arch.Web.IoC;
using Microsoft.Practices.Unity;

namespace Arch.Samples.RiaServices.Web.Infrastructure
{
    public class DependencyContainer : ServerDependencyContainer
    {
        public override void RegisterTypes(IUnityContainer container)
        {
            //Register Repository mapping and ObjectContext creation
            container
                .RegisterType(typeof(IRepository<>), typeof(Repository<>))
                .RegisterType<IRepository, DbRepository>()
                .RegisterType<IPrincipal, IPrincipal>(new InjectionFactory(c => HttpContext.Current.User))
                .RegisterType<DbContext, DbContext>(
                    new PerRequestLifetimeManager<DbContext>(),
                    new InjectionFactory(
                        c =>
                        {
                            var context = new DbContext("name=Arch");
                            context.Configuration.ProxyCreationEnabled = true;
                            context.Configuration.LazyLoadingEnabled = true;
                            //context..LoadFromAssembly(typeof (Todo).Assembly);
                            return context;
                        }))
                ;
            //Register repository interception
            //ConfigureInterception(container.Configure<Interception>().SetDefaultInterceptorFor(typeof(Repository<>), new VirtualMethodInterceptor()));

            //Register Services and their interceptors
            //AutoRegisterTypes(container, typeof (IService<>),
            //                  GetLayerTypes(typeof (EntityService<>), typeof (TodoService).Assembly),
            //                  (i, t) => container.Configure<Interception>()
            //                           .SetDefaultInterceptorFor(typeof(TodoService), new VirtualMethodInterceptor())
            //                           .AddPolicy("FaultHandling").AddCallHandler(new ExceptionToFaultHandler()));

            //Override the default interceptor manager :
            //container.RegisterInstance<IInterceptorManager>(new DefaultInterceptorManager());

            //Override the default faultcontracts mapped :
            //container.RegisterInstance<IEnumerable<IExceptionToFaultHandler>>(new IExceptionToFaultHandler[] { new ValidationExceptionToFaultHandler() });
        }
    }
}